<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\GameServer\DaliyMissionRepository;
use App\Http\Requests\Admin\CreateDaliyMissionRequest;
use App\Http\Requests\Admin\EditDaliyMissionRequest;


class DaliyMissionController extends Controller
{
    protected $Repo;
    protected $type = [
        '局数','赢的局数','押对子命中局数','押和命中局数','天牌胜局数','反胜局数','连胜局数','押庄命中数','押闲命中数','大一点获胜局数','三点以下获胜局数','押庄六点赢局数'
    ];

    protected $icon = [
        '001','002','003','004','005','006'
    ];



    public function __construct(DaliyMissionRepository $repo)
    {
        $this->middleware('admin.auth');
        $this->Repo = $repo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $daliy_missions = $this->Repo->getDaliyMissions();

        return view('admin.daliyMissions.index', compact('daliy_missions','type'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $type = $this->type;
        $icon = $this->icon;

        return view('admin.daliyMissions.create', compact('type', 'icon'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateDaliyMissionRequest $request)
    {
        $res = $this->Repo->addDaliyMission($request);

        if (is_object($res)) {
            $data = [
                'status' => 0,
                'msg'    => '添加成功',
                'url'    => route('daliyMissions.index')
            ];
        } else {
            $data = [
                'status' => 2,
                'msg'    => '添加失败'
            ];
        }
        return json_encode($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $daliyMission = $this->Repo->getDaliyMission($id);
        $type = $this->type;
        $icon = $this->icon;

        return view('admin.daliyMissions.edit', compact('daliyMission','type','icon'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(EditDaliyMissionRequest $request, $id)
    {
        $res = $this->Repo->updateDaliyMission($request, $id);
        $data = [
            'status' => 0,
            'msg'    => '更新成功',
            'url'    => route('daliyMissions.index')
        ];

        return json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = $this->Repo->deleteDaliyMission($id);
        $data = [
            'status' => 0,
            'msg'    => '删除成功',
            'url'    => route('daliyMissions.index')
        ];
        return json_encode($data);
    }

    public function destroyList(Request $request)
    {
        $ids = explode(',', $request->ids);

        if ($request->isMethod('post')) {
            $this->Repo->deleteDaliyMission($ids);
            $data = [
                'status' => 0,
                'msg'    => '删除成功',
                'url'    => route('daliyMissions.index')
            ];
            return json_encode($data);
        }
    }
}
